﻿using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace CXW.Core.Sharp
{
    ///<summary>窗口基类</summary>
    [Designer(typeof(Gui.BaseFormDesigner), typeof(IRootDesigner))]
    public partial class BaseForm : XtraForm
    {
        private string language = "";
        /// <summary>构造函数</summary>
        public BaseForm()
        {
            InitializeComponent();

            base.FormClosing += delegate (object sender, FormClosingEventArgs e)
            {
                if (_confirmExit)
                {
                    if (MessageService.AskQuestion(ResourceService.GetString("Info.ExitConfirm"), "Info.CaptionWarning")) { e.Cancel = false; }
                    else { e.Cancel = true; }
                }
            };
            ResourceService.LanguageChanged += delegate { OnLanguageChanged(); };
        }
        /// <summary>变更UI显示语言时执行此方法</summary>
        public virtual void OnLanguageChanged()
        {
            if (!string.IsNullOrWhiteSpace(Language) && (language = ResourceService.GetString(Language)) != Language)
            {
                Text = language;
            }
        }
        /// <summary>处理命令键。</summary>
        /// <param name="msg">通过引用传递的 System.Windows.Forms.Message，它表示要处理的 Win32 消息。</param>
        /// <param name="keyData">System.Windows.Forms.Keys 值之一，它表示要处理的键。</param>
        /// <returns>如果控件处理并使用击键，则为 true；否则为 false，以允许进一步处理。</returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape://当窗体有GridControl控件时,KeyUp事件会失效,事件无法触发
                        if (_escExit) { this.Close(); }//esc关闭窗体
                        break;
                }
            }
            return false;
        }

        private bool _confirmExit = false;
        /// <summary>指示窗体退出时是否需要确认</summary>
        [CustomCategory, ConfirmExitDescription]
        public bool ConfirmExit
        {
            get { return _confirmExit; }
            set { _confirmExit = value; }
        }
        private bool _escExit = false;
        /// <summary>按Esc时是否退出</summary>
        [CustomCategory, Description("按Esc时是否退出")]
        public bool EscToExit
        {
            get { return _escExit; }
            set { _escExit = value; }
        }
        /// <summary>指示多语言显示的关键字</summary>
        [CustomCategory, Description("指示多语言显示的关键字")]
        public string Language { get; set; }
        /// <summary>指示窗口标题</summary>
        [Browsable(false), CustomCategory, Description("指示窗口标题")]
        public string Title { get { return Text; } }

        /// <summary>获取或设置控件的皮肤名称</summary>
        [CustomCategory, Browsable(false), TypeConverter(typeof(SkinNameTypeConverter)), LookAndFeelSkinNameDescription]
        public string SkinName
        {
            get { return defaultLookAndFeel1.LookAndFeel.SkinName; }
            set { defaultLookAndFeel1.LookAndFeel.SkinName = value; }
        }
        /// <summary>设置皮肤样式</summary>
        /// <param name="skinName"></param>
        internal virtual void SetLookAndFeel(string skinName)
        {
            switch (skinName.ToLower())
            {
                case "flat":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Flat;
                    break;
                case "ultraflat":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.UltraFlat;
                    break;
                case "styled3d":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Style3D;
                    break;
                case "office2003":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Office2003;
                    break;
                case "devexpressstyle":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "DevExpress Style";
                    break;
                case "devexpressdarkstyle":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "DevExpress Dark Style";
                    break;
                case "vs2010":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "VS2010";
                    break;
                case "sevenclassic":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Seven Classic";
                    break;
                case "office2007blue":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2007 Blue";
                    break;
                case "office2007black":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2007 Black";
                    break;
                case "office2007silver":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2007 Silver";
                    break;
                case "office2010blue":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2010 Blue";
                    break;
                case "office2010black":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2010 Black";
                    break;
                case "office2010silver":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2010 Silver";
                    break;
                case "office2013":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2013";
                    break;
                case "office2013darkgray":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2013 Dark Gray";
                    break;
                case "office2013lightgray":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2013 Light Gray";
                    break;
                case "visualstudio2013blue":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Visual Studio 2013 Blue";
                    break;
                case "visualstudio2013light":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Visual Studio 2013 Light";
                    break;
                case "visualstudio2013dark":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Visual Studio 2013 Dark";
                    break;
                case "office2016colorful":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2016 Colorful";
                    break;
                case "office2016dark":
                    defaultLookAndFeel1.LookAndFeel.Style = LookAndFeelStyle.Skin;
                    SkinName = "Office 2016 Dark";
                    break;
                default:
                    break;
            }
        }
    }
}
